package com.yuyu.qingmangmall.product.app;

import com.yuyu.common.utils.PageUtils;
import com.yuyu.common.utils.R;
import com.yuyu.qingmangmall.product.entity.AttrEntity;
import com.yuyu.qingmangmall.product.entity.AttrGroupEntity;
import com.yuyu.qingmangmall.product.service.AttrAttrgroupRelationService;
import com.yuyu.qingmangmall.product.service.AttrGroupService;
import com.yuyu.qingmangmall.product.service.AttrService;
import com.yuyu.qingmangmall.product.service.CategoryService;
import com.yuyu.qingmangmall.product.vo.AttrGroupRelationVo;
import com.yuyu.qingmangmall.product.vo.AttrgropAndAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;



/**
 * 属性分组
 *
 * @author yuwengsheng
 * @email 3337002363@qq.com
 * @date 2021-05-31 01:02:11
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrgroupRelationService;


    /**
     * 查询所有列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 根据条件查询列表
     */
    @RequestMapping("/list/{catelogId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable(value = "catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     *  /product/attrgroup/{attrgroupId}/attr/relation
     *  获取指定分组关联的所有属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrList = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data",attrList);
    }

    /**
     *  获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     *  /product/attrgroup/{attrgroupId}/noattr/relation
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params,
                            @PathVariable("attrgroupId") Long attrgroupId) {


        PageUtils page = attrService.getRelationNoAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    /**
     *  获取分类下所有分组&关联属性
     *  /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public R attrgroupAndAttrs(@PathVariable("catelogId") Long  catelogId) {
        // 1.获得分类下的所有属性分组
        List<AttrgropAndAttrsVo> attrGroupWithAttrsList = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        // 2.通过分类的所有分组，获得分组的所有属性

        return R.ok().put("data",attrGroupWithAttrsList);
    }

    /**
     *   添加属性与分组关联关系
     *  /product/attrgroup/attr/relation
     */
    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrGroupRelationVo> relationVos) {

        attrgroupRelationService.saveBatchRelation(relationVos);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();

        Long [] CatelogPath = categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(CatelogPath);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     *  删除属性与分组的关联关系
     *   /product/attrgroup/attr/relation/delete
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] relationVo) {

        attrGroupService.deleteRelation(relationVo);
        return R.ok();
    }
}
